package leetcodev1.数组;

public class LeetCode35 {

    //实现一个二分查找
    //在一个有序数组中找第一个大于等于target的下标
    public int searchInsert(int[] nums, int target) {
        if (nums == null) {
            return -1;
        }

        int left = 0, right = nums.length - 1, mid, index = nums.length;
        while (left <= right) {
            mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return mid;
            }

            if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
                index = mid;
            }
        }
        return index;
    }
}
